package com.villaday.idf.custom;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.adapter.DialogUpdateGroupAdapter;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.BaseDialog;
import com.villaday.idf.model.vipsGroupsEntity;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * 添加组员界面中修改用户分组提示对话框
 */
@SuppressLint("ValidFragment")
public class UpdateGroupDialog extends BaseDialog implements View.OnClickListener, OkHttpUtil.OnDataListener {
    TextView tvContent;
    RecyclerView recyclerView;
    Context context;
    private String nickName;
    private String memberId;
    private String groupId;

    private List<vipsGroupsEntity.BodyBean> dataList = new ArrayList<>();


    private OnModifyGroupListener mListener;

    private Gson gson = new GsonBuilder().create();
    private DialogUpdateGroupAdapter recyclerViewAdapter;


    public static UpdateGroupDialog newInstance(Context context, String groupId, String nickName, String memberId, String groupStr) {
        UpdateGroupDialog dialog = new UpdateGroupDialog(context);
        Bundle bundle = new Bundle();
        bundle.putString("groupName", nickName);
        bundle.putString("memberId", memberId);
        bundle.putString("groupId", groupId);
        bundle.putString("groupStr", groupStr);
        dialog.setArguments(bundle);
        return dialog;
    }

    public UpdateGroupDialog(Context context) {
        this.context = context;
    }

    @SuppressLint("WrongViewCast")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Window window = getDialog().getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);//必须放在setContextView之前调用
        View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_update_group, null);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        this.setCancelable(false);
        //宽度撑满
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        //进入退出动画
        window.setWindowAnimations(R.style.animTranslateTop);


        tvContent = view.findViewById(R.id.tv_nick_name);
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_group_list);

        nickName = getArguments().getString("groupName");
        memberId = getArguments().getString("memberId");
        groupId = getArguments().getString("groupId");
        tvContent.setText(nickName);
        vipsGroupsEntity vipsGroupsEntity = (gson.fromJson(getArguments().getString("groupStr"), vipsGroupsEntity.class));
        dataList.addAll(vipsGroupsEntity.getBody());

        TextView btnCancel = (TextView) view.findViewById(R.id.tv_cancel_btn);
        TextView btnSaveGroup = (TextView) view.findViewById(R.id.tv_ok);

        btnCancel.setOnClickListener(this);
        btnSaveGroup.setOnClickListener(this);

        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerViewAdapter = new DialogUpdateGroupAdapter(context, groupId);
        recyclerViewAdapter.setOnItemClickListener(new AbsRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                groupId = dataList.get(position).getId();
                for (vipsGroupsEntity.BodyBean bean : dataList) {
                    if (groupId.equals(bean.getId())) {
                        bean.setSelected(true);
                    } else {
                        bean.setSelected(false);
                    }
                }
                recyclerViewAdapter.notifyDataSetChanged();
            }
        });
        recyclerView.setAdapter(recyclerViewAdapter);

/*        Map<String, String> map = new HashMap<>();
        OkHttpUtil.getJSON(context, Constant.URL.vips_groups, map, this);*/
        for (int i = 0; i < dataList.size(); i++) {
            if (groupId.equals(dataList.get(i).getId())) {
                dataList.remove(i);
            }
        }
        recyclerViewAdapter.setData(dataList);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.85), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_ok: {  //保存分组
                if (this.mListener != null) {
                    mListener.modifyGroup(groupId, memberId);
                }
                dismiss();

            }
            break;

            case R.id.tv_cancel_btn: {  //取消分组
                dismiss();
            }
            break;


        }

    }

    @Override
    public void onResponse(String url, String json) {
        if (!TextUtils.isEmpty(json)) {
            LogeUtil.e("vips_groups", json);
            vipsGroupsEntity recommendEntity = gson.fromJson(json, vipsGroupsEntity.class);
            dataList.clear();
            if ("200".equals(recommendEntity.getCode())) {
                dataList.addAll(recommendEntity.getBody());
            }
     /*       for (vipsGroupsEntity.BodyBean bean : dataList) {
                if (groupId.equals(bean.getId())) {
                    bean.setSelected(true);
                } else {
                    bean.setSelected(false);
                }
            }*/
            for (int i = 0; i < dataList.size(); i++) {
                if (groupId.equals(dataList.get(i).getId())) {
                    dataList.remove(i);
                }
            }
            recyclerViewAdapter.setData(dataList);

        }
    }

    @Override
    public void onFailure(String url, String error) {

    }

    public interface OnModifyGroupListener {
        public void modifyGroup(String groupId, String memberId);
    }

    public void setOnModifyGroupListener(OnModifyGroupListener listener) {
        this.mListener = listener;
    }


}
